﻿using System;
using Company.BLL;
using Company.BLL.KDT.GC;
using Company.BLL.Utils;
using Company.BLL.DuLieuChuan;
using System.Data;
using Janus.Windows.GridEX;
using System.Windows.Forms;

namespace Company.Interface.KDT.GC
{
    public partial class SanPhamGCEditForm : BaseForm
    {        
        public SanPham SPDetail;
        public SanPhamCollection spCollection;
        public Company.BLL.KDT.GC.NhomSanPhamCollection nhomSP = new Company.BLL.KDT.GC.NhomSanPhamCollection();
        public HopDong hongdong = new HopDong();
        public bool isAdd = true;
        public bool isUpdate = false;
        private int STTHang = 0;
        public bool isBrower = false;
        public SanPhamGCEditForm()
        {
            InitializeComponent();
            spCollection = new SanPhamCollection();
            SPDetail = new SanPham();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.SPDetail = null;
            this.Close();
        }
        private void reset()
        {
            txtMa.Text = "";
            txtTen.Text = "";
            txtMaHS.Text = "";
            txtSoLuong.Text = "0";          
            dgList.Refetch();
            txtMa.Focus();
            isAdd = true;
            SPDetail = new SanPham();
            btnAdd.Text = "Thêm"; 
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            cvError.Validate();
            // Kiểm tra tính hợp lệ của mã HS.
            if (!MaHS.Validate(txtMaHS.Text, 10))
            {
                error.SetIconPadding(txtMaHS, -8);
                error.SetError(txtMaHS, "Mã số HS không hợp lệ.");
                txtMaHS.Focus();
                return;
            }

            if (!cvError.IsValid) return;
            if (Convert.ToDecimal(txtSoLuong.Text) == 0)
            {
                error.SetError(txtSoLuong, "Số lượng phải lớn hơn 0.");
                txtSoLuong.Focus();
                return;
            }       
            
            checkExitsSanPhamAndSTTHang();    
        }
        private void LoadLoaiSanPham()
        {
            DataTable loaiSp = Company.BLL.KDT.GC.NhomSanPham.SelectBy_HopDong_ID(hongdong.ID).Tables[0];
            cbNhomSP.DataSource = loaiSp;
            cbNhomSP.DisplayMember = "TenSanPham";
            cbNhomSP.ValueMember = "ID";
            if (loaiSp.Rows.Count > 0)
                cbNhomSP.SelectedIndex = 0;
        }
        private void SanPhamGCEditForm_Load(object sender, EventArgs e)
        {
            if (!isBrower)
            {
                txtMa.Focus();
                this._DonViTinh = DonViTinh.SelectAll();
                cbDonViTinh.DataSource = this._DonViTinh;
                cbDonViTinh.DisplayMember = "Ten";
                cbDonViTinh.ValueMember = "ID";
                cbDonViTinh.SelectedValue = GlobalSettings.DVT_MAC_DINH;
                LoadLoaiSanPham();
                if (!isAdd)
                {
                    txtMa.Text = SPDetail.Ma.Trim();
                    txtTen.Text = SPDetail.Ten.Trim();
                    txtMaHS.Text = SPDetail.MaHS.Trim();
                    txtSoLuong.Text = SPDetail.SoLuongDangKy.ToString();
                    cbDonViTinh.SelectedValue = SPDetail.DVT_ID;
                    cbNhomSP.SelectedValue = SPDetail.NhomSanPham_ID.ToString();
                    btnAdd.Text = "Sửa";
                }
                if (hongdong.TrangThaiXuLy == TrangThaiXuLy.DA_DUYET)
                {
                    btnAdd.Enabled = false;
                    uiButton1.Enabled = false;
                }
                foreach (SanPham s in spCollection)
                {
                    if (s.STTHang > STTHang)
                        STTHang = s.STTHang;
                }
            }
            else
            {
                uiGroupBox2.Visible = false;
                dgList.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.False;
                spCollection = SanPham.SelectCollectionBy_HopDong_ID(hongdong.ID);
                this.Width = dgList.Width;
                this.Height = dgList.Height;
                btnAdd.Visible = false;
                uiButton1.Visible = false;
                btnClose.Visible = false;
            }
            dgList.DataSource = spCollection;
              
        }
        private void checkExitsSanPhamAndSTTHang()
        {
            if (!isAdd)
                spCollection.Remove(SPDetail);
            else//lay thong tin tao moi
            {
                SPDetail.STTHang = STTHang + 1;
                SPDetail.SoLuongDangKy = Convert.ToDecimal(txtSoLuong.Text);
                SPDetail.MaHS = txtMaHS.Text.Trim();
                SPDetail.Ten = txtTen.Text.Trim();
                SPDetail.NhomSanPham_ID =(long)Convert.ToUInt64(cbNhomSP.SelectedValue.ToString());
                SPDetail.DVT_ID = cbDonViTinh.SelectedValue.ToString();
                SPDetail.Ma = txtMa.Text.Trim();
                SPDetail.HopDong_ID = hongdong.ID;
            }
           
            foreach (SanPham sp in spCollection)
            {
                if (sp.checkExits(txtMa.Text))
                {
                    if (isAdd)//cap nhat
                    {
                        string st = ShowMessage("Đã có sản phẩm này trong danh sách. Bạn có muốn cập nhật lại không?", true);
                        if (st == "Yes")
                        {
                            sp.Ma = SPDetail.Ma.Trim();
                            sp.Ten = SPDetail.Ten.Trim();                            
                            sp.SoLuongDangKy = SPDetail.SoLuongDangKy;
                            sp.NhomSanPham_ID = SPDetail.NhomSanPham_ID;
                            sp.MaHS = SPDetail.MaHS.Trim();
                            sp.DVT_ID = SPDetail.DVT_ID;                            
                            reset();
                            return;
                        }
                        else
                        {
                            SPDetail = new SanPham();
                            txtMa.Focus();
                            return;
                        }
                    }
                    else
                    {
                        ShowMessage("Đã có sản phẩm này trong danh sách.", false);
                        spCollection.Add(SPDetail);
                        txtMa.Focus();
                        return;
                    }
                }
            }
            if (isAdd)
            {
                spCollection.Add(SPDetail);
                STTHang++;
                reset();
            }
            else//cap nhat
            {                
                SPDetail.SoLuongDangKy = Convert.ToDecimal(txtSoLuong.Text);
                SPDetail.MaHS = txtMaHS.Text.Trim();
                SPDetail.Ten = txtTen.Text.Trim();
                SPDetail.NhomSanPham_ID =(long)Convert.ToUInt64(cbNhomSP.SelectedValue.ToString());
                SPDetail.DVT_ID = cbDonViTinh.SelectedValue.ToString();
                SPDetail.Ma = txtMa.Text.Trim();
                SPDetail.HopDong_ID = hongdong.ID;
                spCollection.Add(SPDetail);
                reset();
                              
            }            
        }
        private void txtMaHS_Leave(object sender, EventArgs e)
        {
            if (!MaHS.Validate(txtMaHS.Text, 10))
            {
                error.SetIconPadding(txtMaHS, -8);
                error.SetError(txtMaHS, "Mã số HS không hợp lệ.");
                txtMaHS.Focus();
            }
           
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
           
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (hongdong.ID <= 0)
                {
                    ShowMessage("Phải lưu trữ hợp đồng trước", false);
                }
                else
                {
                    if (hongdong.TrangThaiXuLy == TrangThaiXuLy.DA_DUYET)
                    {
                        ShowMessage("Hợp đồng đã duyệt chính thức. Không được chỉnh sửa", false);
                        return;
                    }
                    int i = 1;
                    foreach (SanPham s in spCollection)
                    {
                        s.STTHang = i++;
                    }
                    if (SPDetail.InsertUpdate(spCollection))
                    {
                        //spCollection = SanPham.SelectCollectionBy_HopDong_ID(hongdong.ID);
                        ////reset();
                        ////dgList.DataSource = spCollection;
                        isUpdate = true;
                        ShowMessage("Đã cập nhật thành công", false);
                        this.Close();
                    }
                    else
                        ShowMessage("Có lỗi cập nhật", false);
                }
            }
            catch (Exception ex)
            {
                ShowMessage("Có lỗi :" + ex.Message,false);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void dgList_DeletingRecord(object sender, Janus.Windows.GridEX.RowActionCancelEventArgs e)
        {
            if (hongdong.TrangThaiXuLy == TrangThaiXuLy.DA_DUYET)
            {
                ShowMessage("Hợp đồng đã duyệt chính thức. Không được chỉnh sửa",false);
                e.Cancel = true;
                return;
            }
            if (e.Row.RowType == RowType.Record)
            {
                if (ShowMessage("Bạn có muốn xóa sản phẩm này không?", true) == "Yes")
                {
                    SanPham sp1 = new SanPham();
                    sp1.ID = (long)Convert.ToInt64(e.Row.Cells[2].Text);
                    if (sp1.ID > 0)
                    {
                        sp1.Delete();
                        isUpdate = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void dgList_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {
            SPDetail.STTHang = (int)Convert.ToDecimal(e.Row.Cells[1].Text);
            SPDetail.SoLuongDangKy = Convert.ToDecimal(e.Row.Cells[8].Text);
            SPDetail.MaHS = e.Row.Cells[5].Text.Trim();
            SPDetail.Ten = e.Row.Cells[4].Text.Trim();
            SPDetail.NhomSanPham_ID = (long)Convert.ToUInt64(e.Row.Cells[9].Text);
            SPDetail.DVT_ID = e.Row.Cells[6].Text;
            SPDetail.Ma = e.Row.Cells[3].Text.Trim();
            SPDetail.HopDong_ID = hongdong.ID;
            SPDetail.ID = (long)Convert.ToUInt64(e.Row.Cells[2].Text);
            if (!isBrower)
            {
                btnAdd.Text = "Sửa";
                txtMa.Text = SPDetail.Ma.Trim();
                txtTen.Text = SPDetail.Ten.Trim();
                txtMaHS.Text = SPDetail.MaHS.Trim();
                txtSoLuong.Text = SPDetail.SoLuongDangKy.ToString().Trim();
                cbDonViTinh.SelectedValue = SPDetail.DVT_ID;
                cbNhomSP.SelectedValue = SPDetail.NhomSanPham_ID;
                btnAdd.Text = "Sửa";
                isAdd = false;
            }
            else
            {
                this.Close();
            }
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            long id=(long)Convert.ToUInt64(e.Row.Cells[9].Value);
            e.Row.Cells[10].Text = Company.BLL.KDT.GC.NhomSanPham.Load(id).TenSanPham;
            e.Row.Cells[7].Text = this.DonViTinh_GetName(e.Row.Cells[6].Value).Trim();
        }
    }
}